define([ 'backbone',
         'components/pop/base',
         'text!components/pop/templates/change_category2.html',
         'core/utils'],
function( Backbone, Base, template, utils) {
    'use strict';

    return Base.extend({

        template: _.template(template),

        className: 'popover right popover-category-wrap',

        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback',
            'keydown .js-text-placeholder': function(e) {
                if (e.keyCode === utils.keyCode.ENTER) {
                    this.triggerCallback();
                }
            },
            'click .popover-content-categories li': 'handleCategoryClick',
            'mousewheel .js-popover-content-categories': utils.handleScroll
        },

        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            this.data = options.data;
            this.parseData();
        },

        render: function() {
            this.$el.html(this.template({data: this.data}));
            return this;
        },

        setData: function(data) {
            this.data = data;
            this.render();
        },

        handleCategoryClick: function(e) {
            var
                view = this,
                $li = $(e.currentTarget),
                $check = $li.find('.js-category-check'),
                id = '' + $li.data('id'),
                items = view.data.items;

            if($check.hasClass('category-check-all')) {
                $check
                    .removeClass('category-check-all')
                    .removeClass('category-check-portion')
                    .addClass('category-check-none');

                for(var key in items) {
                    if(items.hasOwnProperty(key)) {
                        items[key] = _.without(items[key], id);
                    }
                }
            } else {
                $check
                    .removeClass('category-check-portion')
                    .removeClass('category-check-none')
                    .addClass('category-check-all');

                for(var key in items) {
                    if(items.hasOwnProperty(key)) {
                        items[key].push(id);
                        items[key] = _.union(items[key]);
                    }
                }
            }
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data;

            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.clearInput();
            this.show();
        },

        triggerCallback: function() {
            this.callback(this.data.items);
            this.hide();
        },

        clearInput: function() {
            this.$('.js-link-placeholder').val('');
        },

        parseData: function() {
            var
                data = this.data,
                categories = data.categories,
                items = data.items,
                containsCategory = null;

            _.each(categories, function(category) {
                category.check = 'none';

                containsCategory = function(item) {
                    return _.contains(item, category.id);
                };

                if(_.some(items, containsCategory)) {
                    category.check = 'portion';
                }

                if(_.every(items, containsCategory)) {
                    category.check = 'all';
                }
            });

            data.title = data.title || '分组';
        }
    });
});
